package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Result;
import com.example.demo.entity.Question;
import com.example.demo.mapper.QuestionMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api(tags = "题目访问接口")
@RestController
@CrossOrigin
@RequestMapping("/api/question")
public class QuestionController {
    @Autowired
    private QuestionMapper questionmapper;

    @ApiOperation(value = "获取所有题目信息")
    @GetMapping("/info")
    public Result<?> selectAll(){
        List list = questionmapper.selectList(null);
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到题目信息！");
        }
    }

    @ApiOperation(value = "按页数获取题目信息")
    @GetMapping("/info/page")
    public Result<?> selectByPage(@RequestParam(defaultValue = "1") int num,
                                  @RequestParam(defaultValue = "10") int size,
                                  @RequestParam(defaultValue = "") String str){
        LambdaQueryWrapper<Question> wrapper = Wrappers.<Question>lambdaQuery();
        if (!Objects.equals(str, "")){
            wrapper.like(Question::getCont, str).or().like(Question::getId, str);
        }

        Page page = questionmapper.selectPage(new Page<>(num, size), wrapper);

        if(!page.getRecords().isEmpty()) {
            return Result.success(page);
        } else {
            return Result.error("404", "找不到题目信息！");
        }
    }

    @ApiOperation(value = "根据题目id获取题目信息")
    @GetMapping("/info/id")
    public Result<?> selectById(@RequestParam int id)
    {
        if(String.valueOf(id).equals("")){
            return Result.error("401", "参数不能为空！");
        }
        Question question = questionmapper.selectById(id);
        if(question != null) {
            return Result.success(question);
        } else {
            return Result.error("404", "找不到题目信息！");
        }
    }

    @ApiOperation(value = "搜索题目内容获取题目信息")
    @GetMapping("/info/search")
    public Result<?> search(@RequestParam String str){
        //System.out.println(str);
        LambdaQueryWrapper<Question> wrapper = Wrappers.<Question>lambdaQuery();
        wrapper.like(Question::getCont, str);
        List list = questionmapper.selectList(wrapper);
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到题目信息！");
        }
    }

    @ApiOperation(value = "根据题目类型获取题目信息")
    @GetMapping("/info/qtype")
    public Result<?> selectByQtype(int qtype){
        List list = questionmapper.selectList(Wrappers.<Question>lambdaQuery().eq(Question::getQtype, qtype));
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到题目信息！");
        }
    }

    @ApiOperation(value = "根据选项类型获取题目信息")
    @GetMapping("/info/otype")
    public Result<?> selectByOtype(int otype){
        List list = questionmapper.selectList(Wrappers.<Question>lambdaQuery().eq(Question::getOtype, otype));
        if(!list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "找不到题目信息！");
        }
    }

    @ApiOperation(value = "获取全部记录数量")
    @GetMapping("/info/count")
    // 后端请求地址
    public Result<?> count(){
        int count = questionmapper.selectCount(Wrappers.<Question>lambdaQuery());
        // 对数据库中的数据进行计数
        return Result.success(count);
        // 返回题目数量
    }

    @ApiOperation(value = "添加题目信息")
    @PostMapping("")
    public Result<?> insert(Question question) {
        /*if(question.isEmpty()){
            return Result.error("401", "参数不能为空！");
        }*/
        boolean bool = (questionmapper.insert(question) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "添加题目失败！");
        }
    }

    @ApiOperation(value = "根据题目id删除题目信息")
    @DeleteMapping("")
    public Result<?> delete(@RequestParam int id){
        boolean bool = (questionmapper.deleteById(id) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "删除题目失败！");
        }
    }

    @ApiOperation(value = "根据题目id更新题目信息")
    @PutMapping("")
    public Result<?> update (Question question) {
        boolean bool = (questionmapper.updateById(question) > 0);
        if(bool) {
            return Result.success();
        } else {
            return Result.error("-1", "更新题目失败！");
        }
    }
}